package com.fr.base.xml;

import com.fr.report.io.xml.SynchronizedVersion;
import com.sun.xml.stream.ZephyrParserFactory;
import java.io.Reader;
import java.io.StringReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class XMLableReader
{
  private static final int NULL_STATE = -1;
  private static final int ATTRIBUTE = 0;
  private static final int CHILD_NODE = 1;
  private static final int CHARACTERS = 2;
  private XMLEventReader xmlEventReader;
  private StartElement startElement;
  private XMLEvent currentEvent;
  private int deep = 0;
  private int state = -1;

  public static XMLableReader createXMLableReader(String paramString)
    throws XMLStreamException
  {
    return createXMLableReader(new StringReader(paramString));
  }

  public static XMLableReader createXMLableReader(Reader paramReader)
    throws XMLStreamException
  {
    ZephyrParserFactory localZephyrParserFactory = new ZephyrParserFactory();
    if (localZephyrParserFactory.isPropertySupported("report-cdata-event"))
      localZephyrParserFactory.setProperty("report-cdata-event", Boolean.TRUE);
    else if (localZephyrParserFactory.isPropertySupported("http://java.sun.com/xml/stream/properties/report-cdata-event"))
      localZephyrParserFactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", Boolean.TRUE);
    XMLEventReader localXMLEventReader = localZephyrParserFactory.createXMLEventReader(paramReader);
    XMLableReader localXMLableReader = new XMLableReader(localXMLEventReader);
    if (localXMLableReader.initRootElement() == null)
      return null;
    String str = localXMLableReader.getAttr("xmlVersion");
    if (str != null)
    {
      SynchronizedVersion localSynchronizedVersion = SynchronizedVersion.getSynchronizedVersion(Thread.currentThread());
      localSynchronizedVersion.setXmlVersionByString(str);
    }
    return localXMLableReader;
  }

  private XMLableReader(XMLEventReader paramXMLEventReader)
  {
    this.xmlEventReader = paramXMLEventReader;
  }

  private StartElement initRootElement()
  {
    try
    {
      do
      {
        if (!(this.xmlEventReader.hasNext()))
          break label63;
        this.currentEvent = this.xmlEventReader.nextEvent();
      }
      while (!(this.currentEvent.isStartElement()));
      this.deep += 1;
      this.startElement = this.currentEvent.asStartElement();
      label63: break label63:
    }
    catch (XMLStreamException localXMLStreamException)
    {
      if (this.startElement != null)
        localXMLStreamException.printStackTrace();
    }
    return this.startElement;
  }

  public XMLableReader(XMLEventReader paramXMLEventReader, StartElement paramStartElement)
  {
    this.xmlEventReader = paramXMLEventReader;
    this.startElement = paramStartElement;
    this.currentEvent = this.startElement;
  }

  public void setXMLEventReader(XMLEventReader paramXMLEventReader)
  {
    this.xmlEventReader = paramXMLEventReader;
  }

  public XMLEventReader getXMLEventReader()
  {
    return this.xmlEventReader;
  }

  public String getTagName()
  {
    if (this.startElement == null)
      return null;
    return this.startElement.getName().toString();
  }

  protected static String cdataDecode(String paramString)
  {
    if (SynchronizedVersion.isAfterCPT_XML_Version())
      return XMLEncodeUtils.cdataDecode(paramString);
    return XMLEncodeUtils.deprecatedCDATADecode(paramString);
  }

  public String getElementValue()
  {
    if (!(this.currentEvent.isStartElement()))
      throw new IllegalStateException("current state must be StartElement");
    try
    {
      StringBuffer localStringBuffer = new StringBuffer();
      this.currentEvent = this.xmlEventReader.nextEvent();
      for (int i = this.currentEvent.getEventType(); i != 2; i = this.currentEvent.getEventType())
      {
        if ((this.currentEvent.isCharacters()) && (this.currentEvent.asCharacters().isCData()))
        {
          localStringBuffer.append(cdataDecode(this.currentEvent.asCharacters().getData()));
        }
        else if ((i == 12) || (i == 4) || (i == 6) || (i == 9))
        {
          String str = this.currentEvent.asCharacters().getData();
          if (str.matches("\\s+"))
            break label263:
          localStringBuffer.append(str.trim());
        }
        else if (i != 3)
        {
          if (i == 5)
            break label263:
          if (i == 8)
            throw new XMLStreamException("unexpected end of document when reading element text content");
          if (i == 1)
            throw new XMLStreamException("elementGetText() function expects text only elment but START_ELEMENT was encountered.", this.currentEvent.getLocation());
          throw new XMLStreamException("Unexpected event type " + i, this.currentEvent.getLocation());
        }
        this.currentEvent = this.xmlEventReader.nextEvent();
      }
      label263: this.deep -= 1;
      if (localStringBuffer.length() == 0)
        return null;
      return localStringBuffer.toString();
    }
    catch (XMLStreamException localXMLStreamException)
    {
      localXMLStreamException.printStackTrace();
    }
    return null;
  }

  public boolean isAttr()
  {
    return (this.state == 0);
  }

  public boolean isCharacters()
  {
    return (this.state == 2);
  }

  public boolean isChildNode()
  {
    return (this.state == 1);
  }

  public String getAttr(String paramString)
  {
    if (this.startElement == null)
      throw new IllegalStateException("StartElement should not be null...");
    Attribute localAttribute = this.startElement.getAttributeByName(QName.valueOf(paramString));
    return ((localAttribute == null) ? null : localAttribute.getValue());
  }

  public String getContent()
  {
    if (this.state != 2)
      throw new IllegalStateException("Current State should be Characters...");
    return this.currentEvent.asCharacters().getData();
  }

  public Object readXMLObject(XMLReadable paramXMLReadable)
  {
    if (paramXMLReadable == null)
      throw new NullPointerException();
    int i = this.deep;
    this.state = 0;
    paramXMLReadable.readXML(this);
    this.state = -1;
    if (this.deep < i)
      return paramXMLReadable;
    try
    {
      while (true)
      {
        do
          while (true)
          {
            while (true)
            {
              if (!(this.xmlEventReader.hasNext()))
                break label241;
              this.currentEvent = this.xmlEventReader.nextEvent();
              if (!(this.currentEvent.isStartElement()))
                break;
              this.deep += 1;
              this.startElement = this.currentEvent.asStartElement();
              this.state = 1;
              paramXMLReadable.readXML(this);
              this.state = -1;
            }
            if (!(this.currentEvent.isEndElement()))
              break;
            if (this.deep <= i)
            {
              this.deep -= 1;
              break label241:
            }
            this.deep -= 1;
          }
        while ((!(this.currentEvent.isCharacters())) || ((!(this.currentEvent.asCharacters().isCData())) && (this.currentEvent.asCharacters().getData().equals("\n"))));
        this.state = 2;
        paramXMLReadable.readXML(this);
        this.state = -1;
      }
    }
    catch (XMLStreamException localXMLStreamException)
    {
      label241: throw new RuntimeException(localXMLStreamException);
    }
    return paramXMLReadable;
  }

  public void close()
    throws XMLStreamException
  {
    this.xmlEventReader.close();
  }
}